
// UART 

#include <pic18.h>
#include "UART.h"
#include "macros.h"
//#include "Command.h"

/* void InitUART ( void ) */
/* { */
/* 	TRISB1 = 1;	//TXD and RXD pins must be set as inputs */
/* 	TRISB2 = 1;	 */
/* 	SPEN	= 1;		// UART enable */
/* 	BRGH = 1;		// if this bit is 0, then LOW SPEED */
/* 	RX9 = 0;		// 8Bit */
/* 	TXEN = 0;		// The Transmit is disable if TXEN == 0; */
/* 	CREN = 0;		// The Receive is disable if CREN == 0; */
/* 	SPBRG = 25;	//Bits Rate Transfer == 9600 */
	
/* } */

void putchar ( char ch )
{
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = ch;
}

void puts ( const char *s )
{
	while ( *s )
	{
		putchar( *s++ );
	}	
}

void putsn ( unsigned char *s, unsigned char b_num )
{
	unsigned char i;
	for ( i = 0; i < b_num; i++ )
	{
		putchar(s[i]);
	}		
}


unsigned char UART_Receive_Buffer[UART_RECEIVE_BUFFER_SIZE];
volatile unsigned char pUART_Receive_Buffer;
volatile bit UART_Receive_Complete;

/* UART receiver */

void uart_receiver ( void )
{	
	UART_Receive_Buffer[pUART_Receive_Buffer] = RCREG;

	if ( UART_Receive_Buffer[pUART_Receive_Buffer] == '\r' )
	{

		if ( pUART_Receive_Buffer )
		{
			UART_Receive_Buffer[pUART_Receive_Buffer] = 0;
			UART_Receive_Complete = 1;
		}

		pUART_Receive_Buffer = 0;
	}
	else
	{
		pUART_Receive_Buffer++;
		pUART_Receive_Buffer %= UART_RECEIVE_BUFFER_SIZE;
	}
}

void uart_drv ( void )
{
		/* UART */
	if ( UART_Receive_Complete )
	{
		UART_Receive_Complete = FALSE;

		//Cmd_Add( UART_Receive_Buffer, CMD_SOURCE__UART );
	}
}
